import 'package:flutter/material.dart';

enum BoundaryEdge {
  left,
  right,
  top,
  bottom,
  none,
}
//边界检测器
///如
///  //检查边界
//   void _checkBoundaries() {
//     final contentSize = _getChartRendererSize();
//     final viewportSize = MediaQuery.of(context).size;
//     final boundaries = BoundaryDetector.checkBoundary(
//       matrix: _transformationController.value,
//       contentSize: contentSize,
//       viewportSize: viewportSize,
//     );
//     if (boundaries[BoundaryEdge.left]!) {
//       //到达左边界
//     }
//   }
// Size _getChartRendererSize() {
//     final RenderBox renderBox = _chartRendererKey.currentContext?.findRenderObject() as RenderBox;
//     return renderBox.size;
//   }

class BoundaryDetector {
  static Map<BoundaryEdge, bool> checkBoundary({
    required Matrix4 matrix,
    required Size contentSize,
    required Size viewportSize,
  }) {
    final Rect contentRect = Rect.fromLTWH(0, 0, contentSize.width, contentSize.height);
    final Rect viewportRect = Rect.fromLTWH(0, 0, viewportSize.width, viewportSize.height);
    final Matrix4 inverse = Matrix4.inverted(matrix);
    final Rect transformedContentRect = MatrixUtils.transformRect(matrix, contentRect);
    final Rect transformedViewportRect = MatrixUtils.transformRect(inverse, viewportRect);
    return {
      BoundaryEdge.left: transformedContentRect.left >= transformedViewportRect.left,
      BoundaryEdge.right: transformedContentRect.right <= transformedViewportRect.right,
      BoundaryEdge.top: transformedContentRect.top >= transformedViewportRect.top,
      BoundaryEdge.bottom: transformedContentRect.bottom <= transformedViewportRect.bottom,
    };
  }
}